package com.yuju.controller.group;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.group.enums.GuestInfoReturnCodeEnum;
import com.yuju.group.model.GuestInfoModel;
import com.yuju.group.service.IGuestInfoService;
import com.yuju.util.Utils;
import com.yuju.vo.group.GuestInfoVO;
/**
 * 
 * 类名：HotelController  <br />
 *
 * 功能：
 *
 * @author : sunyl <br />
 * @Date : 2017年11月24日 上午9:40:05  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("guestInfo")
public class GuestInfoController extends BaseController<GuestInfoVO, GuestInfoModel> {
	@Autowired
	private IGuestInfoService guestInfoService;
	
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject jsonObject)   {
		GuestInfoVO GuestInfoVO = jsonObject.toJavaObject(GuestInfoVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(GuestInfoVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		PageModel pageModel = guestInfoService.pageList(convert(GuestInfoVO), GuestInfoVO.getPage(), GuestInfoVO.getRows());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GuestInfoReturnCodeEnum.GUESTINFO_QUERY_SUCCESS.getCode());
			result.setReturnMsg(GuestInfoReturnCodeEnum.GUESTINFO_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject jsonObject) {
		GuestInfoVO GuestInfoVO = jsonObject.toJavaObject(GuestInfoVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(GuestInfoVO, "save", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = guestInfoService.save(convert(GuestInfoVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GuestInfoReturnCodeEnum.GUESTINFO_SAVE_SUCCESS.getCode());
			result.setReturnMsg(GuestInfoReturnCodeEnum.GUESTINFO_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(GuestInfoReturnCodeEnum.GUESTINFO_SAVE_FAILURE.getCode());
			result.setReturnMsg(GuestInfoReturnCodeEnum.GUESTINFO_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject jsonObject) {
		GuestInfoVO GuestInfoVO = jsonObject.toJavaObject(GuestInfoVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(GuestInfoVO, "update", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Map<String, String> failSave = FieldValidator.validateAll(GuestInfoVO, "save", true);
		if (!failSave.isEmpty()) {
			return validateError(failSave);
		}
		Result result = new Result();
		int rows = guestInfoService.updateSelective(convert(GuestInfoVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GuestInfoReturnCodeEnum.GUESTINFO_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(GuestInfoReturnCodeEnum.GUESTINFO_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(GuestInfoReturnCodeEnum.GUESTINFO_UPDATE_FAILURE.getCode());
			result.setReturnMsg(GuestInfoReturnCodeEnum.GUESTINFO_UPDATE_FAILURE.getDesc());
		}
		return result;
	}

	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject jsonObject) {
		GuestInfoVO GuestInfoVO = jsonObject.toJavaObject(GuestInfoVO.class);
		Map<String, String> fail = FieldValidator.validateAll(GuestInfoVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = guestInfoService.del(GuestInfoVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GuestInfoReturnCodeEnum.GUESTINFO_DEL_SUCCESS.getCode());
			result.setReturnMsg(GuestInfoReturnCodeEnum.GUESTINFO_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(GuestInfoReturnCodeEnum.GUESTINFO_DEL_FAILURE.getCode());
			result.setReturnMsg(GuestInfoReturnCodeEnum.GUESTINFO_DEL_FAILURE.getDesc());
		}
		return result;
	}

	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject jsonObject) {
		GuestInfoVO GuestInfoVO = jsonObject.toJavaObject(GuestInfoVO.class);
		Result result = new Result();
		GuestInfoModel GroupModel = guestInfoService.find(convert(GuestInfoVO));
		if (Utils.isNotNull(GroupModel)) {
			result.setData(GroupModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GuestInfoReturnCodeEnum.GUESTINFO_QUERY_SUCCESS.getCode());
			result.setReturnMsg(GuestInfoReturnCodeEnum.GUESTINFO_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
}
